package br.com.miboco.patterns.observer;

import java.util.ArrayList;
import java.util.List;

public class Observable {
	private List<Observer> observers;

	public synchronized void deleteObservers() {
		getObservers().clear();
	}

	public synchronized void addObserver(Observer o) {
		getObservers().add(o);
	}

	public synchronized int countObservers() {
		return getObservers().size();
	}

	public synchronized void deleteObserver(Observer o) {
		getObservers().remove(o);
	}

	private List<Observer> getObservers() {
		if (observers == null)
			observers = new ArrayList<Observer>();
		return observers;
	}

	protected void broadcastObservers() {
		for (Observer o : getObservers())
			o.update();
	}
}
